﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Yipusoft.MapInfo.BLL;
using Yipusoft.MapInfo.Model;

public partial class setMap : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            string strUrl = "";
            foreach (FlagInfo flag in MapImage.GetFlagListByMap(Request["img"]))
            {
                strUrl += flag.X.ToString() + "," + flag.Y.ToString() + "," + flag.Type + "," + flag.FlagImage + ";";
            }
            MarkupImage.ImageUrl="Markup.aspx?img="+Request["img"]+"&desc="+strUrl;
           
            // Bind the dropdown list
            //DataTable tbl = GetMarkTypeTable();
            //listType.DataSource = tbl;
            //listType.DataValueField = "value";
            //listType.DataTextField = "desc";
            //listType.DataBind();

            // Create javascript for displaying example text
            //string script = "<script language=javascript> function getExample() {";
            //script += "var examples = new Array(" + tbl.Rows.Count.ToString() + ");";
            //for (int i = 1; i < tbl.Rows.Count; i++)
            //{
            //    script += "examples[" + tbl.Rows[i]["value"] + "]=\"" + tbl.Rows[i]["example"] + "\";";
            //}
            //script += "document.getElementById(\"lblExample\").innerHTML=";
            //script += "examples[document.getElementById(\"listType\").value]; }";
            //script += "</script>";
            //Session["ExampleScript"] = script;
        }

        // Register client-side javascript for displaying example text
        //string javascript = Session["ExampleScript"] as string;
        //if (javascript != null)
        //{
        //    Page.RegisterStartupScript("getExample", javascript);
        //    listType.Attributes.Add("onChange", "getExample()");
        //}

        // Retrieve x and y where the mouse click took place
        string x = Request.Params["MarkupImage.x"];
        string y = Request.Params["MarkupImage.y"];
        // Get type of annotation selected
        string type = listType.SelectedValue;
        // Get any applicable argument
        //DataTable tbl1 = GetMarkTypeTable();
        string param = "";
        //param = hfFile.Value;
        //param = txtParam.Text;
        // Create the MarkableType
        MarkableType mark = MarkManager.CreateByID(type);
        if (mark != null && param.Length > 0)
        {
            mark.LoadFromString(param);
            
        }
        
        // Construct the new ImageURL
        if (x != null && y != null && x.Length >= 0 && y.Length >= 0 && mark != null)
        {
            FlagInfo flag = new FlagInfo();
            flag.BGMap = Request["img"];
            flag.X = int.Parse(x);
            flag.Y = int.Parse(y);
            flag.Type = type;
            flag.FlagImage = mark.SaveAsString();
            MapImage.InsertFlag(flag);
            string append = string.Join(",", new string[] { x, y, type, mark.SaveAsString() });
            MarkupImage.ImageUrl += HttpUtility.HtmlEncode(append + ";");
        }
    }

   
 
    protected void btnRemove_Click(object sender, EventArgs e)
    {
        if (txtRemove.Text.Length <= 0)
        {
            return;
        }

        string url = HttpUtility.HtmlDecode(MarkupImage.ImageUrl);

        int id = -1;
        try
        {
            id = int.Parse(txtRemove.Text);
        }
        catch { }

        int pos1 = url.IndexOf("&desc=") + 5;
        int pos2 = -1;
        // pos1 is to the right of pos2
        while (id >= 0)
        {
            pos2 = pos1;
            pos1 = url.IndexOf(";", System.Math.Max(0, pos2 + 1)); // non-negative
            if (pos1 >= 0)
            {
                id--;
            }
            else
            {
                // did not find the id
                return;
            }
        }

        // discard between pos2+1 and pos1, inclusive
        MapImage.DeleteFlag(int.Parse(txtRemove.Text),Request["img"]);
        string imageUrl = url.Remove(pos2 + 1, pos1 - pos2);
        if (imageUrl.ToLower() == "marku")
        {
            MarkupImage.ImageUrl = "Markup.aspx?img=" + Request["img"] + "&desc=";
        }
        else
            MarkupImage.ImageUrl = imageUrl;
    }
    
}